package net.gamebug.guessthemovie;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import java.util.Random;

public class Game extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.game);
        
        Global.n = new bd(this);
        Global.rn = new Random();
        Global.n.open();
        Global.counter = new counter(30000, 1000);
        startGame();
    }

    public void startGame() {
        //imagens invisiveis ate load
        hideImages();
        randomizeButtons();
        //start counter
        Global.counter.start();
        Typeface font = Typeface.createFromAsset(getAssets(), "verdana.ttf");

        final Button movie1 = (Button) findViewById(R.id.btnmovie1);
        movie1.setTypeface(font);
        final Button movie2 = (Button) findViewById(R.id.btnmovie2);
        movie2.setTypeface(font);
        final Button movie3 = (Button) findViewById(R.id.btnmovie3);
        movie3.setTypeface(font);
        final Button movie4 = (Button) findViewById(R.id.btnmovie4);
        movie4.setTypeface(font);

      
        //introduz o texto
        
        //novo random para escolher 1 filme 
        String[] arr = new String[4];
        arr[0] = Global.nn;
        arr[1] = Global.n2;
        arr[2] = Global.n3;
        arr[3] = Global.n4;
        int escolha = Global.rn.nextInt(4);
        //escreve nome teste
        final String escolhatxt = arr[escolha].toString();
        TextView arg = (TextView) findViewById(R.id.txt2);
        arg.setText(escolhatxt);
        List<String> imagensfilmes = Global.n.getAllImages(escolhatxt);
        String imagem1 = imagensfilmes.get(0);
        String imagem2 = imagensfilmes.get(1);
        String imagem3 = imagensfilmes.get(2);
        int resID1 = getResources().getIdentifier(imagem1, "drawable", getPackageName());
        int resID2 = getResources().getIdentifier(imagem2, "drawable", getPackageName());
        int resID3 = getResources().getIdentifier(imagem3, "drawable", getPackageName());
        ImageView img1 = (ImageView) findViewById(R.id.imageView1);
        img1.setImageResource(resID1);
        Global.img2.setImageResource(resID2);
        Global.img3.setImageResource(resID3);

        movie1.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                String filme1texto = movie1.getText().toString();
                if (escolhatxt.equals(filme1texto)) {
                    
                    nextLevel(escolhatxt);
                    
                } else {
			boolean response = Global.n.checkIfInTop5(Global.score);
                   if(response==true){
                	   Intent myinIntent = new Intent(v.getContext(), Gameover.class);
                	   startActivityForResult(myinIntent, 0);
                	   Global.counter.cancel();
                	   Cleardata n= new Cleardata();
                	   n.clearData();
                	   finish();
                	   
                   }else{
                	   Intent myinIntent = new Intent(v.getContext(),Justgameover.class);
                	   startActivityForResult(myinIntent, 0);
                	   Global.counter.cancel();
                	   Cleardata n= new Cleardata();
                	   n.clearData();
                	   finish();}


            }
            }});

        movie2.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                String filme2texto = movie2.getText().toString();
                if (escolhatxt.equals(filme2texto)) {
                    
                    nextLevel(escolhatxt);
                    
                } else {
			boolean response=Global.n.checkIfInTop5(Global.score);

			  if(response==true){
				  Intent myinIntent = new Intent(v.getContext(), Gameover.class);
				  startActivityForResult(myinIntent, 0);
                  Global.counter.cancel();
                  Cleardata n= new Cleardata();
                  n.clearData();
                  finish();

			  }else{
				  Intent myinIntent = new Intent(v.getContext(),Justgameover.class);
				  startActivityForResult(myinIntent, 0);
                  Global.counter.cancel();
                  Cleardata n= new Cleardata();
                  n.clearData();
                  finish();}



}
            }
        });

        movie3.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                String filme3texto = movie3.getText().toString();
                if (escolhatxt.equals(filme3texto)) {
                    
                    nextLevel(escolhatxt);
                   
                } else {
		boolean response=Global.n.checkIfInTop5(Global.score);

				if(response==true){
					Intent myinIntent = new Intent(v.getContext(), Gameover.class);
					startActivityForResult(myinIntent, 0);
                    Global.counter.cancel();
                    Cleardata n= new Cleardata();
                    n.clearData();
                    finish();

				}else{
					Intent myinIntent = new Intent(v.getContext(),Justgameover.class);
					startActivityForResult(myinIntent, 0);
                    Global.counter.cancel();
                    Cleardata n= new Cleardata();
                    n.clearData();
                    finish();}
                    
                }
            }
        });

        movie4.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                String filme4texto = movie4.getText().toString();
                if (escolhatxt.equals(filme4texto)) {
                    
                    nextLevel(escolhatxt);
                    
                } else {
		boolean response=Global.n.checkIfInTop5(Global.score);

                  if(response == true){
                	  Intent myinIntent = new Intent(v.getContext(), Gameover.class);
                	  startActivityForResult(myinIntent, 0);
                      Global.counter.cancel();
                      Cleardata n= new Cleardata();
                      n.clearData();
                      finish();

                  }else{
                	  Intent myinIntent = new Intent(v.getContext(),Justgameover.class);
                	  startActivityForResult(myinIntent, 0);
                      Global.counter.cancel();
                      Cleardata n= new Cleardata();
                      n.clearData();
                      finish();}


            }
            }});
    }

    public void nextLevel(String filmecr) {
        
       TextView scoore = (TextView) findViewById(R.id.txtscore);
        Global.count=Global.count+1;
             if (Global.count <= 3) {
       
                
                
                
                if(Global.numsec>=1 && Global.numsec<=10){   
                Global.score=10*Global.numsec+Global.score;
                scoore.setText(Long.toString(Global.score));
                }
                
                if(Global.numsec>=11 && Global.numsec<=20){ 
                 Global.score=20*Global.numsec+Global.score;
                   scoore.setText(Long.toString(Global.score));
                }
                 
                if(Global.numsec>=21 && Global.numsec<=30){
                    
                 Global.score=30*Global.numsec+Global.score;
                  scoore.setText(Long.toString(Global.score));
                }
                
                
                
                Global.counter.cancel();
                Global.filmessairam.add(filmecr);
                Global.firstrun = false;
                
                if (Global.count == 3) {
                    
                    Intent win = new Intent(this, Win.class);
                    startActivityForResult(win, 0);
                    finish();
                    Cleardata n= new Cleardata();
                           n.clearData(); 
                    
                } else startGame();
                
            
        }
        

   
    }

    public void randomizeButtons() {
        //gets all movies from database
        Global.filmes = Global.n.getAllMovieNames();
        //size of filmes array
        int size = Global.filmes.size();
        //random botao 1
        //if first run , get the first one , if not keeps randomizing until it gets a film that isnt on the list filmessairam
        if (Global.firstrun == false) {
            do {
                Global.num = Global.rn.nextInt(size);
                

            } while (Global.filmessairam.contains(Global.filmes.get(Global.num)));
        } else {
            Global.num = Global.rn.nextInt(size);
            Global.nn = Global.filmes.get(Global.num);
        }
        
        Global.nn = Global.filmes.get(Global.num);
        //set text in the button
        final Button movie1 = (Button) findViewById(R.id.btnmovie1);
        movie1.setText(Global.nn);
        
        
        //random botao 2
        do {
            Global.num2 = Global.rn.nextInt(size);
            
        } while (Global.num2 == Global.num || Global.filmessairam.contains(Global.filmes.get(Global.num2)));

        final Button movie2 = (Button) findViewById(R.id.btnmovie2);
        Global.n2 = Global.filmes.get(Global.num2);
        movie2.setText(Global.n2);
        //random botao 3
        do {
            Global.num3 = Global.rn.nextInt(size);
        } while (Global.num3 == Global.num || Global.num3 == Global.num2 || Global.filmessairam.contains(Global.filmes.get(Global.num3)));

        final Button movie3 = (Button) findViewById(R.id.btnmovie3);
        Global.n3 = Global.filmes.get(Global.num3);
        //introduz o texto
        movie3.setText(Global.n3);
        //random botao 4
        do {
            Global.num4 = Global.rn.nextInt(size);
        } while (Global.num4 == Global.num || Global.num4 == Global.num2 || Global.num4 == Global.num3 || Global.filmessairam.contains(Global.filmes.get(Global.num4)));
        final Button movie4 = (Button) findViewById(R.id.btnmovie4);
        Global.n4 = Global.filmes.get(Global.num4);
        //introduz o texto
        movie4.setText(Global.n4);
    }

    public class counter extends CountDownTimer {

        public counter(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            finish();
            Cleardata n= new Cleardata();
            n.clearData();
        }

        @Override
        public void onTick(long millisUntilFinished) {
            TextView counterr = (TextView) findViewById(R.id.tvcnt);
            Global.numsec = millisUntilFinished / 1000;
            if (Global.numsec == 20) {
                ImageView img2 = (ImageView) findViewById(R.id.imageView2);
                img2.setVisibility(View.VISIBLE);
            }
            if (Global.numsec == 10) {
                ImageView img3 = (ImageView) findViewById(R.id.imageView3);
                img3.setVisibility(View.VISIBLE);
            }
            Global.time = Long.toString(Global.numsec);
            counterr.setText(Global.time);
        }
    }

    public void hideImages() {
        Global.img2 = (ImageView) findViewById(R.id.imageView2);
        Global.img2.setVisibility(View.INVISIBLE);
        Global.img3 = (ImageView) findViewById(R.id.imageView3);
        Global.img3.setVisibility(View.INVISIBLE);
    }
    
    @Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		android.os.Process.killProcess(android.os.Process.myPid());
	}
   
}